/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-22 上午11:24
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.bean.User;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.config.Constant;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.NetUtils;
import com.example.dqhl.jdmetro.utils.StringFormatUtil;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.utils.UserSaveUtil;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:登录Activity
 * autour:guanluocang
 * date:17/6/23 09:50
 */
public class LoginActivity extends BaseActivity {

    private Context context;
    @Bind(R.id.et_userName)
    EditText et_userName;
    @Bind(R.id.et_editPassword)
    EditText et_editPassword;
    @Bind(R.id.btn_hidePassword)
    Button btn_hidePassword;
    @Bind(R.id.tv_login)
    TextView tv_login;
    @Bind(R.id.tv_loginReigster)
    TextView tv_loginReigster;
    @Bind(R.id.tv_loginForgetPass)
    TextView tv_loginForgetPass;
    @Bind(R.id.iv_loginWeChat)
    ImageView iv_loginWeChat;
    @Bind(R.id.iv_loginQQ)
    ImageView iv_loginQQ;

    private boolean isShow = true;

    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        context = this;
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        tv_login.setOnClickListener(this);
        btn_hidePassword.setOnClickListener(this);
        tv_loginReigster.setOnClickListener(this);
        tv_loginForgetPass.setOnClickListener(this);
        iv_loginQQ.setOnClickListener(this);
        iv_loginWeChat.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //显示隐藏密码
            case R.id.btn_hidePassword:
                hideOrShowPass();
                break;
            //登录按钮
            case R.id.tv_login:
                doLogin();
                break;
            //忘记密码
            case R.id.tv_loginForgetPass:
                JumpItent.jump(LoginActivity.this, ForgetPasswordActivity.class);
                break;
            //注册
            case R.id.tv_loginReigster:
                JumpItent.jump(LoginActivity.this, RegisterActivity.class);
                break;
            //WeChat
            case R.id.iv_loginWeChat:
                Toastutils.show("WeChat");
                break;
            //QQ
            case R.id.iv_loginQQ:
                Toastutils.show("QQ");
                break;
        }

    }

    //登录
    public void doLogin() {
        String userName = et_userName.getText().toString().trim();
        String password = et_editPassword.getText().toString().trim();
        String passOne = MD5.md5(password);
        String passTwo = MD5.md5(passOne + passOne);
        String passThree = MD5.md5(passTwo + "~!@#$%^&*()_+");
        if (TextUtils.isEmpty(userName)) {
            Toastutils.show("请输入账号");
        } else if (TextUtils.isEmpty(password)) {
            Toastutils.show("请输入密码");
        } else if (!StringFormatUtil.cellCheck(userName)) {
            Toastutils.show("请输入正确手机号");
        } else if (!NetUtils.isConnected(this)) {
            Toastutils.show("网络连接异常");
        } else {
            showLoadingBar("");
            RequestParams params = new RequestParams(Config.login);
            params.addBodyParameter("account", userName);
            params.addBodyParameter("password", passThree);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    String data = JsonUtils.getData(result);
                    if (errCode == 200) {
                        User user = JSON.parseObject(data, User.class);
                        UserSaveUtil.saveUser(context, user);
                        /**
                         * 登录成功的广播
                         * */
                        Intent intent = new Intent(Constant.ACTION_LOGIN_SUCCESS);
                        intent.putExtra("user", user);
                        sendBroadcast(intent);
                        finish();
                    } else {
                        Toastutils.show(errMsg);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideLoadingBar();
                }
            });

        }
    }

    //密码的显示与隐藏
    public void hideOrShowPass() {
        if (isShow) {
            et_editPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            btn_hidePassword.setBackgroundResource(R.drawable.ic_login_show);
        } else {
            et_editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            btn_hidePassword.setBackgroundResource(R.drawable.ic_login_hide);
        }
        isShow = !isShow;
        et_editPassword.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence charSequence_confirm = et_editPassword.getText();
        if (charSequence_confirm instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence_confirm;
            Selection.setSelection(spanText, charSequence_confirm.length());
        }
    }
}
